
#include <gtest/gtest.h>
#include <StringHasher.h>
#include <iostream>
#include <cstdio>
using namespace std;
using namespace tagfolder;

class StringHasherTest: public ::testing::Test{
    protected:
};

void NonDuplicateTest( HashAlg<const char*>& alg){
    const char *str1="HelloWorld";
    const char *str2="HelloWorld";
    ASSERT_EQ( alg(str1), alg(str2));

    const char *str3="WorldHello";
    ASSERT_NE( alg(str1), alg(str3));
}

TEST_F( StringHasherTest, HashAlgorithmTest){
    SDBHash sdb;
    RSHash rs;
    JSHash js;
    PJWHash pjw;
    ELFHash elf;
    APHash ap;

    NonDuplicateTest( sdb);
    NonDuplicateTest( rs);
    NonDuplicateTest( js);
    NonDuplicateTest( pjw);
    NonDuplicateTest( elf);
    NonDuplicateTest( ap);
}

TEST_F( StringHasherTest, StringHashValueTest){
    const char *strings[]={
        "12342553874398758927483798473824723984",
        "asdfghjklmn",
        "asdfghjklmn",
        "bsdfghjklmn",
    };

    StringHasher& hasher=StringHasher::getInstance();
    unsigned long hash;
    for( int i=0;i<4;++i){
        hash=hasher.makeHash( strings[i]);
        EXPECT_NE( hash, 0);
    }

    unsigned long hash0=hasher.makeHash( strings[0]);
    unsigned long hash1=hasher.makeHash( strings[1]);
    unsigned long hash2=hasher.makeHash( strings[2]);
    unsigned long hash3=hasher.makeHash( strings[3]);
    cout<<"hash 0,1,2,3 = "<<hash0<<"\t"<<hash1<<"\t"<<hash2<<"\t"<<hash3<<endl;
    ASSERT_EQ( hash1, hash2);
    ASSERT_NE( hash1, hash3);
}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
